In the last couple of videos we extensively worked with rolling statistics.
For instance we calculated the simple moving average of stock prices and also the rolling mean return
and the rolling standard deviation of returns and actually all examples.
For one thing in common they're rolling statistics I calculated on one time series.
So for example on the Apple stock price and in this video we will learn how to calculate rolling statistics
like correlation or the covariance where we need to operate on two time series.
So for example we could to calculate the rolling correlation of Apple monthly returns with the Boeing
monthly returns.
So now let's start here and as always import pandas and met plot lip and we use the Seabourn style and
then we import our six stocks that we have in our stock so yes we fire and we create a daytime index
and we only select here the close column.
So let's do this and let's have a look here.
So these are our stocks.
Apple Boeing Disney IBM Coca-Cola and Microsoft.
And now in the following minutes we want to calculate the rolling correlation between the Apple stock
and the Boeing stock.
And therefore we only select the apple and Boeing and from our time period from end of 2009 and the
end of 2000 and 18 and we actually created a new data frame and called the data frame.
Apple Boeing.
So let's do this and let's have a look.
So here we have the two columns Apple and Boeing with the stock prices.
And I want a first step we calculate the monthly returns and therefore first of all we have to re sample
the data frame to a monthly and we pass a period to the kind parameter and we take actually the very
last the trading price of each month and then with the percentage change method we actually calculate
the simple returns and we are actually saving.
The new returns data frame and the variable monthly returns.
So let's have a look.
So here we have the monthly returns for Apple and Boeing starting in January 2010 and December 2000
and 18.
And that's also the a look here with the info method.
So we have a total one hundred eight monthly returns for nine years times twelve months gives us one
hundred eight monthly returns and then we could simply calculate the correlation between the apple return
and the Boeing return for the whole nine year period.
So by using here the com method let's do this year and no surprise there that we have a perfect positive
correlation between the Apple stock and the Apple stock and the Boeing stock and the Boeing stock.
But here we can see uh the correlation of the last nine years.
The monthly returns between Apple and Boeing.
And this is the kind of positive so point to two.
And I wouldn't say that this is a strong positive correlation but still it's a significant uh positive
correlation.
So that's the significant trend that whenever the apple return is positive.
Then also the Boeing return is positive and vice versa.
So this is the correlation for the complete one hundred eight month period and the same with the returns
and standard deviations.
Let's assume that you want to calculate the rolling correlation.
And also here we can define that.
We want to have a window of 36 months.
So it's quite common in the finance industry to calculate return and standard deviation outlaw correlation
based on monthly returns of uh the last uh 36 six months and coding wise we can actually start to do
so by selecting here the apple return.
Then we apply the rolling method and we pass the thirty six uh to the windows parameter and we already
know that.
Now we can for example calculate the rolling mean return by chaining here.
The mean method.
So this is nothing new.
So this is the three years uh rolling mean return for the Apple stock.
And we also have calculated the rolling standard deviation of returns by chaining here the standard
deviation method.
So this is actually nothing new.
And now we want to calculate that the rolling correlation and it's actually no surprise that we have
to change the car method and inside the parenting is this.
Now we have to decide for which time seriously want to have the role in correlation to the Apple return.
And if we can simply pass the panda series with the Boeing returns.
So it's here monthly return the data frame and then we select uh the Boeing returns.
And now let's run here the cell and it's actually no surprise that here for the first the thirty five
data points we have any end values.
But then here in the very end we have actually the thirty six month rolling correlation between the
Apple stock return and the Boeing stock return.
And as in most cases the graphs are more intuitive than a lot of numbers.
So therefore we can also plot here and visualize a reading role rolling correlation.
So we use here the plot method and let's have a look.
So this is of the year all in correlation between APA returns and Boeing returns.
And we have seen before that the average over the nine year period is 0 point 2 to so the average C
is somewhere here.
But we can also see here that the correlation is quite volatile.
So we have fear at the end of the year 2000 and 13.
Even a negative correlation.
And here into a 16 and 17 we have a correlation of 0 point 5 and actually currently we are quite close
to the mean of 0 point to 2.
So here we are currently somewhere at 0 point 1 8 0.01 7.
And actually the key message is here.
Same with return and risk that also the correlation heavily depends on the market environment and on
the timing.
So these metrics are actually quite volatile over time.
And having only a look at one single timestamp might not give us the complete information.
So it's also quite it to have a look at the rolling statistics and with this we are finished here and
I hope to see you also in the next video by.